JAVA: Summary / Structures 103
3
3
.
.
5
5
S
S
t
t
r
r
u
u
c
c
t
t
u
u
r
r
e
e
s
s
I
I
n
n
f
f
o
o
Content
Classes
Interfaces
Value Enumerator
C
C
l
l
a
a
s
s
s
s
e
e
s
s
Class can have: Properties, Constructors and Methods.
Syntax
class Person { ... }
Basic Syntax
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//DECLARE PROPERTIES.
String name;
int age;
//DECLARE CONSTRUCTOR.
Person(String name, int age) {
this.name = name;
this.age = age;
}
//DECLARE METHOD.
void sayHello() {
System.out.println(name + " is " + age + " years old");
}
}
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Person john = new Person("John", 20); //Create Class Instance by calling its Constructor
john.sayHello(); //Call Method: John is 20 years old
john.name = "Bill"; //Change Property
String name = john.name; //Read Property: Bill
}
}
JAVA: Summary / Structures 104
E
E
x
x
t
t
e
e
n
n
d
d
C
C
l
l
a
a
s
s
s
s
Child Class can extend single Parent Class by adding extends ParentClassName after Child Class Name.
Child Class can reference Parent's Properties and Methods as they were their own.
You can use Keyword super to reference Parent's Properties and Methods when Child has Properties and Methods with
the same name and signature like: super.name, super.displayName().
Use @Override Annotation just to indicate to Compiler your intention to override existing Parent's Method.
Extend Class
//===========================================================================================================
//CLASS: Person
//===========================================================================================================
class Person {
String name = "John";
Integer age = 20;
void displayName() { System.out.println("Person " + name); }
void greetPerson() { System.out.println(name + " is Person"); }
}
//===========================================================================================================
//CLASS: Soldier
//===========================================================================================================
class Soldier extends Person {
String name = "John the killer";
@Override //Intention to override Parent's Method
void displayName () { System.out.println("Soldier " + super.name); }//Reference Parent's Property
void greetSoldier() { super.displayName(); }//Reference Parent's Method
}
//===========================================================================================================
//CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Soldier john = new Soldier(); //Create Instance of Class Soldier
john.displayName(); //Reference Child's Method.
john.greetSoldier(); //Reference Child's Method.
john.greetPerson(); //Reference Parent's Method (inherited).
Integer age = john.age; //Reference Parent's Property (inherited).
}
}
JAVA: Summary / Structures 105
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
s
s
Interface is collection of Methods used by end User to communicate with the Class that must implement that Interface.
This ensures that all classes that implement the same interface have the same set of functionality (interface).
Interface methods are by default
ā— public (since they are intended to be exposed to the end user)
ā— abstract (since prior Java 8 Interface could only have Abstract methods without implementation)
Class that implements Interface
ā— must override Abstract methods (those without implementation)
ā— can override Default methods (optional)
ā— shouldn't override Static methods (forbidden)
Interfaces were introduced to avoid Multiple inheritance problem which occurs when Class can extend multiple Classes.
So in Java Class can only extend one Class but it can implement multiple interfaces.
M
M
e
e
t
t
h
h
o
o
d
d
s
s
Abstract method
ā— can't have implementation defined inside Interface (Interface implementation is forbidden)
ā— must be overridden in each Class that Implements Interface (Class implementation is mandatory)
Default method
ā— must have implementation defined inside Interface (Interface implementation is mandatory)
ā— don't need to be overridden in the Class that Implements Interface (Class implementation is optional)
Static method
ā— must have implementation defined inside Interface (Interface implementation is mandatory)
ā— can't be overridden in the Class that Implements Interface (Class implementation is forbidden)
ā— can only be called in a static way (through Interface name)
A
A
b
b
s
s
t
t
r
r
a
a
c
c
t
t
c
c
l
l
a
a
s
s
s
s
e
e
s
s
v
v
s
s
i
i
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
s
s
After Java 8 difference is that abstract class can have constructor while interface can’t.
Syntax
interface InterfaceA { }
interface InterfaceB { }
class Person implements InterfaceA, InterfaceB { }
JAVA: Summary / Structures 106
Syntax
//===========================================================================================================
// INTERFACE: InterfaceA
//===========================================================================================================
interface InterfaceA {
abstract void abstractMethod();
static void staticMethod () { System.out.println("staticMethod()"); }
}
//===========================================================================================================
// INTERFACE: InterfaceB
//===========================================================================================================
interface InterfaceB {
default void defaultMethod1() { System.out.println("defaultMethod1()"); }
default void defaultMethod2() { System.out.println("defaultMethod2()"); }
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person implements InterfaceA, InterfaceB {
public void abstractMethod() { System.out.println("Overriden abstractMethod()" ); }
public void defaultMethod1() { System.out.println("Overriden defaultMethod1()" ); }
}
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String args[]) {
//CLASS
Person john = new Person();
john.abstractMethod();
john.defaultMethod1();
john.defaultMethod2();
//INTERFACE
InterfaceA.staticMethod();
}
}
JAVA: Summary / Structures 107
V
V
a
a
l
l
u
u
e
e
E
E
n
n
u
u
m
m
e
e
r
r
a
a
t
t
o
o
r
r
Value Enumerator is
ā— Data Type which contains String Constants
ā— used when at compile time we know all possible discrete values/states variable can have
Syntax
//===========================================================================================================
//ENUM: Day
//===========================================================================================================
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
//===========================================================================================================
//CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
//DECLARE VARIABLE day OF ENUM DATA TYPE Day.
Day day = Day.MONDAY; //Asign enum constant to the Variable
//DISPLAY VARIABLE.
System.out.println(day); //MONDAY
//CHECK VALUE USING IF.
if(day == Day.MONDAY ) { System.out.println(day); }
if(day.name() == "MONDAY") { System.out.println(day); }
if(day.toString() == "MONDAY") { System.out.println(day); }
//CHECK VALUE USING SWITCH.
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
}